﻿using Dycox.ComponentModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace Dycox.Linq
{
    public abstract class QueryArgs : ICloneable
    {
        public QueryArgs()
        {

        }

        public virtual IListItem[] GetStandardItems(QueryProperty property)
        {
            return null;
        }

        private int _pageIndex;

        public int PageIndex
        {
            get { return _pageIndex; }
            set
            {
                if (value < 0)
                    throw new ArgumentOutOfRangeException();

                _pageIndex = value;
            }
        }

        private int _pageSize;

        public int PageSize
        {
            get { return _pageSize; }
            set
            {
                if (value < 0)
                    throw new ArgumentOutOfRangeException();

                _pageSize = value;
            }
        }

        public bool EnablePaging
        {
            get { return _pageSize > 0; }
        }

        //public bool EnableSort
        //{
        //    get { return Sort.Count > 0; }
        //}

        private SortOrderSettings _sort;

        public SortOrderSettings Sort
        {
            get
            {
                return _sort;
            }
            set
            {
                _sort = value;
            }
        }

        protected virtual SortOrderSettings DefaultSort
        {
            get { return null; }
        }

        public SortOrderSettings GetSort()
        {
            SortOrderSettings sos = _sort;

            if (sos == null || !sos.HasAnyOrder)
                sos = DefaultSort;

            if (sos != null) return sos.Clone();
            else return null;
        }

        public virtual QueryArgs Clone()
        {
            QueryArgs obj = (QueryArgs)this.MemberwiseClone();
            obj._pageIndex = 0;

            if (this._sort != null)
                obj._sort = this._sort.Clone();

            return obj;
        }

        object ICloneable.Clone()
        {
            return this.Clone();
        }

        public void Reset()
        {
            foreach (PropertyDescriptor pd in TypeDescriptor.GetProperties(this))
            {
                QueryArgAttribute qaa = (QueryArgAttribute)pd.Attributes[typeof(QueryArgAttribute)];

                if (qaa != null)
                    ResetProperty(pd);
            }

            _pageIndex = 0;
        }

        protected virtual void ResetProperty(PropertyDescriptor property)
        {
            if (!property.CanResetValue(this))
            {
            }
            else
                property.ResetValue(this);
        }

        private ModelView _view;

        /// <summary>
        /// Gets or sets a <see cref="Dycox.ComponentModel.ModelView"/> for trasforming data.
        /// </summary>
        public ModelView View
        {
            get { return _view; }
            set { _view = value; }
        }

    }

}
